package cn.mcch.investigator.fragment;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.DatePicker;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.mcch.investigator.R;
import cn.mcch.investigator.application.InvestigatorApplication;
import cn.mcch.investigator.customview.pulltorefresh.PullToRefreshLayout;
import cn.mcch.investigator.dialog.ExitDialog;
import cn.mcch.investigator.dialog.LoadingDialog;
import cn.mcch.investigator.listener.OnResponseListener;
import cn.mcch.investigator.model.bean.CancelRescueBean;
import cn.mcch.investigator.model.bean.MyTaskBean;
import cn.mcch.investigator.model.impl.MyTaskModelImpl;
import cn.mcch.investigator.observer.InvestigatorObserver;
import cn.mcch.investigator.observer.InvestigatorObserverable;
import cn.mcch.investigator.presenter.impl.MyTaskPresenterImpl;
import cn.mcch.investigator.utils.ConstantUtil;
import cn.mcch.investigator.utils.DialogUtils;
import cn.mcch.investigator.utils.JsonUtils;
import cn.mcch.investigator.utils.ToastUtils;
import cn.mcch.investigator.viewdelegate.impl.MyTaskViewDelegateImpl;

/**
 * Created by Administrator on 2016/12/17.
 */
public class MyTaskFragment extends BaseFragment<MyTaskViewDelegateImpl, MyTaskModelImpl, MyTaskPresenterImpl> implements OnResponseListener, InvestigatorObserver, DatePickerDialog.OnDateSetListener {

    ExitDialog cancelRescueDialog;
    private LoadingDialog loadingDialog;
    private int selectDate;

    @Override
    public Class<MyTaskPresenterImpl> getPresenterClass() {
        return MyTaskPresenterImpl.class;
    }

    @Override
    public void createFirstView() {
        InvestigatorObserverable.addObserver(this);
        mPresenter.createFirstView(getActivity().getApplicationContext(), R.layout.layout_loading, this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        InvestigatorObserverable.deleteObserver(this);
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter.page = 1;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("user_id", InvestigatorApplication.getInstance().getUserBeanbean().user_id);
        params.put("page", String.valueOf(mPresenter.page));
        mPresenter.initData(ConstantUtil.ACT_ORDER_LIST, params, getContext(), this, R.layout.fragment_my_task);

    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        mPresenter.page = 1;
        mPresenter.getMoreData(1, this);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        mPresenter.page++;
        mPresenter.getMoreData(2, this);
    }

    @Override
    public void onBefore(int id) {

    }

    @Override
    public void onAfter(boolean result, int id) {
        switch (id) {
            case 1:
                // 下拉刷新
                mPresenter.setRefreshResult(result ? PullToRefreshLayout.SUCCEED : PullToRefreshLayout.FAIL);
                break;
            case 2:
                // 上拉加载更多
                mPresenter.setLoadMoreResult(result);
                if (!result) {
                    // 加载失败，当前页数减1，重新加载
                    mPresenter.page--;
                }
                break;
            case 3:
            case 4:
                // 取消案件结束
                //  搜索结束
                loadingDialog.dismiss();
                loadingDialog = null;
                if (id == 4 && !result) {
                    // 搜索失败，清空数据
                    mPresenter.clearData();
                    mPresenter.setNoMoreData(true);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(String json, int id) {
        switch (id) {
            case 1:
            case 4:
                // 下拉刷新
                mPresenter.setRefreshData(JsonUtils.fromJsonToObject(json, MyTaskBean.class));
                // 下拉刷新成功，否加载完所有数据
                mPresenter.isNoMoreData();
                break;
            case 2:
                mPresenter.setMoreData(JsonUtils.fromJsonToObject(json, MyTaskBean.class));
                // 判断是否加载完所有数据
                mPresenter.isNoMoreData();
                break;
            case 3:
                // 取消案件成功，刷新界面:
                mPresenter.page = 1;
                mPresenter.getMoreData(1, this);
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.sb_exit) {
            // 取消案件救援
            cancelRescueDialog.dismiss();
            cancelRescueDialog = null;
            loadingDialog = DialogUtils.showLoadingDialog(getFragmentManager(), "正在取消，请稍后...");
            mPresenter.cancelRescue(3, cancelRescueBean.order_id, cancelRescueBean.reason, cancelRescueBean.other, this);
        } else if (v.getId() == R.id.sb_find_start) {
            // 开始时间
            selectDate = 1;
            DialogUtils.showDateDialog(getContext(), this);

        } else if (v.getId() == R.id.sb_find_end) {
            // 结束时间
            selectDate = 2;
            DialogUtils.showDateDialog(getContext(), this);

        } else if (v.getId() == R.id.sb_find) {
            // 搜索
            loadingDialog = DialogUtils.showLoadingDialog(getFragmentManager(), "正在查询数据，请稍后...");
            mPresenter.page = 1;
            mPresenter.getMoreData(4, this);

        }
    }

    /**
     * 取消救援的实体
     */
    private CancelRescueBean cancelRescueBean;

    @Override
    public void notify(int type, Object data) {
        if (type == 400) {
            // 取消救援
            cancelRescueBean = (CancelRescueBean) data;
            cancelRescueDialog = DialogUtils.showExitDialog(getFragmentManager(), "确定要取消本案件救援吗？", "取消", "确定", this);

        }
    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        mPresenter.setDate(selectDate, year, monthOfYear, dayOfMonth);
    }
}
